<?php

class str {

    public $html_charset, $strall;

    public function __construct($strall, $html_charset = 'utf-8') {
        $this->strall = $strall;
        $this->html_charset = $html_charset;
    }

    public static function getstr($strall, $str1, $str2) {//从前面开始找
        $str = new str($strall);
        return($str->getstr1($str1, $str2));
    }

    public function getstr1($str1, $str2) {//从前面开始找
        $i1 = mb_strpos($this->strall, $str1, 0, $this->html_charset);
        if (!is_int($i1)) {
            return '';
        }//str1没找到！
        $i1R = $i1 + mb_strlen($str1, $this->html_charset);
        $i2 = mb_strpos($this->strall, $str2, $i1 + mb_strlen($str1, $this->html_charset), $this->html_charset);
        if (!is_int($i2)) {
            return '';
        }//str2都没找到！
        //echo "i1={$i1},i2={$i2}<br>";
        return(mb_substr($this->strall, $i1R, $i2 - $i1R, $this->html_charset));
    }

    public function getstr2($str1, $str2) {//从后面开始找
        if ($str2 != "") {
            $i2 = mb_strrpos($this->strall, $str2, 0, $this->html_charset);
        }
        if (!is_int($i2)) {
            return '';
        }//str2都没找到！
        if ($str1 != "") {
            $i1 = mb_strrpos($this->strall, $str1, -(mb_strlen($this->strall, $this->html_charset) - $i2), $this->html_charset);
        }
        if (!is_int($i1)) {
            return '';
        }//str1没找到！
        return mb_substr($this->strall, $i1 + mb_strlen($str1, $this->html_charset), $i2 - $i1 - mb_strlen($str1, $this->html_charset));
    }

    public static function left($str, $i) {
        return(substr($str, 0, $i));
    }

}
